<template>
  <el-container>
    <el-header>
      <el-form :inline="true" class="form-inline">
        <el-form-item>
          <!-- label="模糊查询" -->
          <el-input v-model="params.keywords" placeholder="部门名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button v-auth="1020301" type="primary" icon="el-icon-search" @click="handleSearch()">查询</el-button>
          <el-button v-auth="1020302" type="primary" icon="el-icon-plus" @click="handleAdd()">新增</el-button>
        </el-form-item>
      </el-form>
    </el-header>
    <el-main class="nopadding">
      <scTable ref="table" :apiObj="listData" v-bind:params="params" row-key="id" @selection-change="selectionChange" stripe border hidePagination>
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column label="部门名称" prop="name" width="250"></el-table-column>
        <el-table-column label="排序" prop="sort" width="150"></el-table-column>
        <el-table-column label="备注" prop="remarks" width="380"></el-table-column>
        <el-table-column label="操作" align="left">
          <template #default="scope">
            <el-button-group>
              <el-button v-auth="1020303" text type="primary" size="small" icon="el-icon-user" class="btnGree" @click="handleUser(scope.row, scope.$index)">用户</el-button>
              <el-button v-auth="1020304" text type="primary" size="small" icon="el-icon-search" @click="handleShow(scope.row, scope.$index)">查看</el-button>
              <el-button v-auth="1020305" text type="primary" size="small" icon="el-icon-edit" class="btnBlue1" @click="handleEdit(scope.row, scope.$index)">编辑</el-button>
              <el-popconfirm title="确定删除吗？" @confirm="handleRemove(scope.row, scope.$index)">
                <template #reference>
                  <el-button v-auth="1020306" type="text" size="small" icon="el-icon-remove" class="btnRed">删除</el-button>
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>

  <save-dialog v-if="dialog.save" ref="saveDialog" @success="callBack" @closed="dialog.save = false"></save-dialog>
  <!-- 人员设置弹窗 -->
  <userset-dialog v-if="dialog.userset" ref="usersetDialog"  @closed="dialog.userset = false"></userset-dialog>
  
</template>

<script>
import saveDialog from "./components/save";
import usersetDialog from "./components/userset";
export default {
  name: "dept",
  components: {
    saveDialog,
    usersetDialog
  },
  data() {
    return {
      params: {
        keywords: "",
      },
      listData: this.$API.department.getTreeList, //表格返回信息
      selection: [],
      dialog: {
        save: false,
        userset: false,
      },
    };
  },
  methods: {
    //获取数据
    handleSearch() {
      this.$refs.table.loadData(this.params);
    },
    //查看
    handleShow(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("show").setData(row);
      });
    },
    //添加
    handleAdd() {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open().getGroup();
      });
    },
    //编辑
    handleEdit(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("edit").setData(row);
      });
    },
    //单条删除
    async handleRemove(row, index) {
      var reqData = { id: row.id };
      var res = await this.$API.department.deleteById.delete(reqData);
      if (res.code == 200) {
        //这里需要重新获取，删除的是树结构
        this.$refs.table.reloadData();
        this.$Message.success("删除成功");
      } else {
        this.$Message.warning(res.msg);
      }
    },
    //表格选择后回调事件
    selectionChange(selection) {
      this.selection = selection;
    },
    //人员设置
    async handleUser(row, index) {
      this.dialog.userset = true;
      this.$nextTick(() => {
        this.$refs.usersetDialog.open(row);
      });
    },
    //本地更新数据
    callBack(data, mode) {
      //不管是编辑、新增，都需要重新获取。【编辑的时候，变了上级，就需要重新获取】
      this.$refs.table.reloadData();
      // if (mode == "add") {
      //   this.$refs.table.reloadData();
      // } else if (mode == "edit") {
      //   this.$refs.table.tableData
      //     .filter((item) => item.id === data.id)
      //     .forEach((item) => {
      //       Object.assign(item, data);
      //     });
      // }
    },
  },
};
</script>

<style></style>
